﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Deppton.Mobile.Views;

namespace Deppton.Mobile
{
    public class CredentialsController
    {
        public CredentialsController(IServicesPortfolio servicesPortfolio)
        {
            this.ServicesPortfolio = servicesPortfolio;
        }

        private IServicesPortfolio ServicesPortfolio
        {
            get;
            set;
        }

        public bool GetCredentials(out string userName, out string password)
        {
            var credentialsObtained = this.GetNewCredentials(out userName, out password);

            return credentialsObtained;
        }

        private bool GetNewCredentials(out string userName, out string password)
        {
            userName = null;
            password = null;

            using (var credentialsForm = new EnterCredentialsForm())
            {
                var credentialsFormResult = credentialsForm.ShowDialog();

                if (credentialsFormResult == System.Windows.Forms.DialogResult.OK)
                {
                    if (this.ServicesPortfolio.CredentialsService.ValidateCredentials(credentialsForm.UserName, credentialsForm.Password))
                    {
                        userName = credentialsForm.UserName;
                        password = credentialsForm.Password;

                        return true;
                    }
                }
            }

            return false;
        }
    }
}
